package com.example.apidocserver.infrastructure.util.entity;

import com.alibaba.druid.util.StringUtils;
import com.example.apidocserver.domain.document.entity.ApiParamField;
import org.springframework.util.CollectionUtils;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;

import static com.example.apidocserver.infrastructure.constants.SystemConstants.SEPARATOR_FIELD;

/**
 * 字段说明工具
 *
 * @author xyqlg
 */
public class ApiFieldUtil {

    /**
     * 字段key，不区分接口
     *
     * @param apiParamField 字段说明对象信息
     * @return fieldKey
     */
    public static String getFieldKey(ApiParamField apiParamField) {
        return apiParamField.getFieldName() + SEPARATOR_FIELD + apiParamField.getFieldTypeId() + SEPARATOR_FIELD
                + apiParamField.getSpaceId();
    }

    /**
     * 字段唯一key，区分接口
     *
     * @param apiParamField 字段说明对象信息
     * @return fieldUniKey
     */
    public static String getFieldUniKey(ApiParamField apiParamField) {
        return apiParamField.getFieldName() + SEPARATOR_FIELD + apiParamField.getFieldTypeId() + SEPARATOR_FIELD
                + apiParamField.getSpaceId() + SEPARATOR_FIELD + apiParamField.getApiId();
    }

    /**
     * 复制非key值
     *
     * @param fieldDb  数据库对象
     * @param fieldNew 新数据对象
     */
    public static void copyValueNoKey(ApiParamField fieldDb, ApiParamField fieldNew) {
        fieldNew.setId(fieldDb.getId()).setApiId(fieldDb.getApiId()).setSpaceId(fieldDb.getSpaceId())
                .setFieldDesc(fieldDb.getFieldDesc()).setFieldMust(fieldDb.getFieldMust())
                .setEnableFlag(fieldDb.getEnableFlag());

        // 非空才赋值
        if (Objects.nonNull(fieldDb.getOrderNo())) {
            fieldNew.setOrderNo(fieldDb.getOrderNo());
        }
        if (Objects.nonNull(fieldDb.getCreateTime())) {
            fieldNew.setCreateTime(fieldDb.getCreateTime());
        }
        if (Objects.nonNull(fieldDb.getCreator())) {
            fieldNew.setCreator(fieldDb.getCreator());
        }
        if (Objects.nonNull(fieldDb.getUpdater())) {
            fieldNew.setUpdater(fieldDb.getUpdater());
        }
        if (Objects.nonNull(fieldDb.getUpdateTime())) {
            fieldNew.setUpdateTime(fieldDb.getUpdateTime());
        }
        if (Objects.nonNull(fieldDb.getUsePriority())) {
            fieldNew.setUsePriority(fieldDb.getUsePriority());
        }
    }

    /**
     * 参数字段排序
     *
     * @param fields 参数字段集合
     */
    public static void sort(List<ApiParamField> fields) {
        if (!CollectionUtils.isEmpty(fields)) {
            fields.sort(Comparator.comparing(ApiParamField::getOrderNo));
        }
    }

    public static void setDescOut(ApiParamField field) {
        Integer descMode = field.getDescMode();
        if (Objects.isNull(descMode)) {
            field.setFieldDescOut(field.getFieldDesc());
        }
        switch (descMode) {
            case 0:
                if (StringUtils.isEmpty(field.getParamFieldDesc())) {
                    field.setFieldDescOut(field.getFieldDesc());
                } else {
                    field.setFieldDescOut(field.getFieldDesc() + "\n" + field.getParamFieldDesc());
                }
                break;
            case 1:
                if (StringUtils.isEmpty(field.getParamFieldDesc())) {
                    field.setFieldDescOut(field.getFieldDesc());
                } else {
                    field.setFieldDescOut(field.getParamFieldDesc() + "\n" + field.getFieldDesc());
                }
                break;
            case 2:
                if (StringUtils.isEmpty(field.getParamFieldDesc())) {
                    field.setFieldDescOut(field.getParamFieldDesc());
                } else {
                    field.setFieldDescOut(field.getFieldDesc());
                }
                break;
            default:
                field.setFieldDescOut(field.getFieldDesc());
        }
    }
}
